<template>
  <div class="hyydxq">
    <div class="listSearch">
      <div>
        <img src="@/assets/img/sou.png" alt="" class="sou">
        <input type="text" v-model="params.earLabelNo" @keyup.enter="onSearch"  placeholder="请输入关键词进行搜索">
        <img src="@/assets/img/cha.png" alt="" @click="params.earLabelNo = ''" class="cha">
      </div>
      <img src="@/assets/img/rili.png" alt="" class="rili" @click="dateShow = true">
    </div>
    <van-calendar :min-date="minDate" :max-date="maxDate" v-model="dateShow" type="range" @confirm="onConfirm" color="#37B5B5" />
    <!-- 预约信息 -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" :finished-text="list.length > 0 ? '没有更多了' : ''" @load="onLoad">
        <div v-if="list.length > 0">
          <van-cell v-for="(item, key) in list" :key="key" class="approvalItem content" @click="byPregnancyRecord(item.id)">
          <div class="flex-between font-14 line-height-3">
            <div class="col-333333 font-bold-500 font-15 m-r-10">{{!!item.individualInfo && item.individualInfo.earLabelNo || '' }}</div>
            <div class="col-999999 line-text-out text-right">{{item.lambDate | dateFormat('YYYY-MM-DD')}}</div>
          </div>
          <div class="flex-between font-13 line-height-3">
            <div class="col-666666 m-r-10 line-text-out">产羔数量：公：{{item.maleLambNum}}
              /母：{{item.femaleLambNum}}/弱羔数：{{item.weakLambNum}}
              /死胎数：{{item.stillbirthNum}}
            </div>
          </div>
          <div class="flex-between font-13 line-height-3">
            <div class="col-666666 m-r-10 line-text-out">健壮羔羊数量：公：{{item.robustMaleLambNum}}/母：{{item.robustFemaleLambNum}}</div>
          </div>
          <div class="flex-between font-13 line-height-3">
            <div class="col-666666 m-r-10 line-text-out">创建人：{{ item.createdByName || '' }}</div>
          </div>
          <div class="flex-between font-13 line-height-3">
            <div class="col-666666 m-r-10 line-text-out">创建时间：{{ item.createTime || '' }}</div>
          </div>
        </van-cell>
        </div>
        <temporarily-data v-else />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
//保养记录
import { getValue, setValue, clearValue} from '@/utils/auth'
import { Toast, Cell, CellGroup, Calendar, Popup, Picker, DatetimePicker, PullRefresh, CheckboxGroup, List, Search} from 'vant'
import { getHandle, postHandle } from '@/api'
import dayjs from 'dayjs'
import TemporarilyData from '@/components/temporarilyData'
export default {
  components: {
    TemporarilyData,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [PullRefresh.name]: PullRefresh,
    [CheckboxGroup.name]: CheckboxGroup,
    [Toast.name]: Toast,
    [List.name]: List,
    [Search.name]: Search,
    [Calendar.name]: Calendar
  },
  data() {
    return {
      startDate: '',
      endDate: '',
      showPicker1: false,
      refreshing: false,
      currentDate: new Date(),
      list: [],
      loading: false,
      finished: false,
      dateShow: false, // 日期弹出框
      minDate: new Date(2011, 1, 1),
      maxDate: new Date(2031, 12, 31),
      params: {
        page: 1,
        perPage: 10,
        startDate: '',
        endDate: '',
        earLabelNo: '',
        houseInfoId:getValue('fzmHourseId')
        // houseInfoName: '羔羊舍'
        // equipmentId: getValue('id')
      }
    }
  },
  watch: {},
  created() {},
  mounted() {
    this.$bridge.register('refresh', data => {
      this.refreshing = true
      this.params.page = 1
      this.onLoad()
      clearValue('cgObj')
      clearValue('earTag2d')
      clearValue('fzmSex')
    })
  },
  methods: {
    byPregnancyRecord(id){
        setValue('cgId', id)
      this.$bridge.call('lambingRecordDetails')
    },
    timeSelection(e) {
      this.showPicker1 = true
      this.flag = e
    },
    onConfirm(date) {
      console.log(date)
      const [start, end] = date
      this.dateShow = false
      this.params.startDate = dayjs(start).format('YYYY-MM-DD')
      this.params.endDate = dayjs(end).format('YYYY-MM-DD')
      this.refreshing = true
      this.params.page = 1
        this.onLoad()
    },
    // onConfirmStartDate(value) {
    //   let timestamp = dayjs(value).valueOf()
    //   if (this.flag == 1) {
    //     if (timestamp <= dayjs(this.params.endDate || '2031-12-31').valueOf()) {
    //       this.params.startDate = dayjs(value).format('YYYY-MM-DD')
    //     } else {
    //       Toast('不得晚于结束时间');
    //       return;
    //     }
    //   } else {
    //     if (timestamp >= dayjs(this.params.startDate || '1987-1-1').valueOf()) {
    //       this.params.endDate = dayjs(value).format('YYYY-MM-DD')
    //     } else {
    //       Toast('不得早于开始时间');
    //       return;
    //     }
    //   }
    //   if (!!this.params.startDate && !!this.params.endDate) {
    //     this.refreshing = true
    //     this.onLoad()
    //   }
    //   this.showPicker1 = false
    // },
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onSearch() {
      this.params.page = 1
      this.list = []
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onLoad() {
      getHandle('/agri/api/yzbzh/lambingRecord/queryListPage', this.params).then(res => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        this.list = this.list.concat(res.data.rows)
        this.loading = false

        if (this.list.length >= res.data.count) {
          this.finished = true
        } else {
          this.params.page = this.params.page + 1
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.topDate {
  width: 80%;
  margin: auto;
  display: flex;
  justify-content: space-between;
  > p {
    font-size: 15px;
    &:first-child {
      width: 40%;
    }
    &:last-child {
      width: 40%;
    }
  }
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
.b-b-2{
  margin-top: 65px;
}
::v-deep .van-search{
  padding-top:0 ;
    width: 98%;
    margin-left: 1%;
    // margin: auto;
}
::v-deep .van-search__content{
    background-color: #fff;
    border-radius: 34px;

}
.approvalItem{
  margin-top: 0px;
  margin-bottom:10px ;
}
.listSearch {
  display: flex;
  align-items: center;
  padding: 10px 18px;
  box-sizing: border-box;
  justify-content: space-between;
  div {
    width: 310px;
    height: 30px;
    position: relative;
    background-color: #fff;
    border-radius: 16px;
    input {
      height: 100%;
      box-sizing: border-box;
      position: absolute;
      left: 28px;
      width: calc(100% - 50px);
    }
    img {
      width: 12px;
      height: 12px;
      position: absolute;
      top: 50%;
      transform: translate(0, -50%);
    }
    .sou {
      left: 10px;
    }
    .cha {
      right: 10px;
    }
  }
  .rili {
    width: 20px;
    height: 20px;
  }
}
</style>
